package computerdatabase;


import io.gatling.javaapi.core.*;
import io.gatling.javaapi.http.HttpProtocolBuilder;

import static io.gatling.javaapi.core.CoreDsl.*;
import static io.gatling.javaapi.http.HttpDsl.*;
import static io.gatling.javaapi.http.HttpDsl.ws;


/**
 * This sample is based on our official tutorials:
 * <ul>
 *   <li><a href="https://gatling.io/docs/gatling/tutorials/quickstart">Gatling quickstart tutorial</a>
 *   <li><a href="https://gatling.io/docs/gatling/tutorials/advanced">Gatling advanced tutorial</a>
 * </ul>
 */
public class AlphaYijianSimulation extends Simulation {

    FeederBuilder<String> feeder = csv("alpha.csv").random();
    HttpProtocolBuilder httpProtocol =
            http.baseUrl("http://www8.alpha.daofengdj.com")
                    .wsBaseUrl("ws://dj_im.alpha.daofengdj.com")
                    .wsAutoReplySocketIo4()
                    .wsReconnect().wsMaxReconnects(5);

    ChainBuilder login = feed(feeder)
            .exec(
                http("mobileLogin")
                        .post("/pw/app/api/v2/mobileLogin.json")
                        .formParam("area_num","86")
                        .formParam("mobile","#{Phone}")
                        .formParam("password","")
                        .formParam("message_code","9999")
                        .formParam("registration_JGID","190e35f7e096081f854")
                        .formParam("source","301")
                        .check(jsonPath("$.code").ofInt().is(1),
                                jsonPath("$.data.id").saveAs("lbUid"),
                                jsonPath("$.data.token").saveAs("lbToken"))
            ).pause(1)
            .exec(ws("WebScoket连接").connect("")).pause(1)
            .exec(ws("webSocket登录")
                    .sendText("{\"action\": \"login\", \"token\": \"#{lbToken}\", \"termtyp\": \"android\", \"data\": {\"form\": \"from_login\"}}")
                    .await(2)
                    .on(ws.checkTextMessage("检查登录状态")
                            .check(jsonPath("$.action")
                                    .ofString()
                                    .is("login_succeed")))
            )
            .exec(ws("Close WS").close());

    ScenarioBuilder users = scenario("Users").exec(login);

    {
        setUp(
//            users.injectOpen(rampUsers(2).during(0))
                users.injectOpen(atOnceUsers(1))
        ).protocols(httpProtocol);
    }
}
